const format = require("../../../utils/format.js")
const app = getApp()
let formIdArr = []
Page({
 
    /**
     * 页面的初始数据
     */
    data: {
        popGency: false,
        deposit: null,
        balance: null,
        depositPercent: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // options.depositPercent = app.gloabData.paramConfig.find(o=>o.) //保证金比例
        options.depositPercent = wx.getStorageSync('TICKET_QUOTE_PRICE_PERCENT') //保证金比例
        this.setData(options)
        this.loadData()
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        Promise.all([
            app.http.post('rest/ticket/queryById.do', {
                data: this.data.id
            }),
            app.http.post('rest/member/queryById.do')
        ]).then(res => {
            this.setData(Object.assign({
                balance: res[1].obj.money, //余额
            }, res[0].obj))
            wx.hideLoading()
        }).catch(err => {
            console.log(err, 'err')
        })
    },
    checkPopGency() {
        this.setData({
            popGency: !this.data.popGency
        })
    },
    // offerBlur(evt) {
    //     let value = evt.detail.value
    //     let offer = this.toDecimal2(value)
    //     this.setData({
    //         offer: offer ? offer : null,
    //     })
    // },
    offerInput(evt) {
        let value = evt.detail.value
        let deposit = parseInt(value, 10) * parseFloat(this.data.depositPercent)
        this.setData({
            offer: value ? value : null,
            deposit: deposit ? Math.ceil(deposit) : null
        })
    },
    // toDecimal2(x) {
    //     let f = parseFloat(x)
    //     if (isNaN(f)) return
    //     f = Math.round(x * 100) / 100
    //     let s = f.toString()
    //     let rs = s.indexOf('.')
    //     if (rs < 0) {
    //         rs = s.length
    //         s += '.'
    //     }
    //     while (s.length <= rs + 2) {
    //         s += '0'
    //     }
    //     return s
    // },
    // formatFloat(f, digit) {
    //     var m = Math.pow(10, digit);
    //     return Math.round(f * m, 10) / m;
    // },
    submit(evt) {
        let {
            deposit = null, balance = null, offer = null, depositPercent = null, remark = ''
        } = this.data
        remark = evt.detail.value.remark
        if (!offer) {
            app.wxToast.warn('请填写报价金额')
            return
        }
        if (offer < 1) {
            app.wxToast.warn('金额不能小于1元')
            return
        }
        if (parseInt(balance, 10) < parseInt(deposit, 10) || isNaN(parseInt(balance))) {
            wx.showModal({
                title: '保证金不足，无法报价',
                content: '保证金为报价' + parseFloat(depositPercent) * 100 + '%',
                showCancel: false,
                confirmText: '去充值',
                confirmColor: '#4DB4FF',
                success: res => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/common/recharge',
                        })
                    }
                }
            })
            return
        }
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('rest/quote/offer.do', {
            data: {
                ticketId: this.data.id,
                price: parseInt(offer, 10),
                remark
            }
        }).then(res => {
            wx.hideLoading()
            wx.redirectTo({
                url: './offer-success?id=' + this.data.id + '&cid=' + res.obj,
            })
        })
    },
    /**
     * 获取formId
     */
    getFormId: function (e) {
        let formId = e.detail.formId
        let time = new Date().getTime() + 7 * 24 * 60 * 60 * 1000
        if (formId != 'the formId is a mock one') {
            formIdArr.push({
                formId: formId,
                expiresTime: format.dateformat.format(new Date(time), "yyyy-MM-dd hh:mm:ss")
            })
        }
        console.log(formIdArr, 'formidarr')
        if (formIdArr.length == 10) {
            app.getFormId(formIdArr)
        }
    },
})